<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/9
 * Time: 14:41
 */
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Db;
class Examine extends Common {
	//登录判断
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 商品审核
	 * Method Get
	 * @param string get.title 要检索的字段
	 * @return Array  $result 需要审核的商品
	*/
	public function index()
	{
		$db = Db::table("product")->order('id desc')->where('status','=','3');
		$where = ['query' =>[]];
		if(!empty(input('get.title'))){
			$title = input('get.title');
			//多条件检索
			$db->where("pname|purchaseprice|retailprice|bid","like","%{$title}%");
			$where['query']['title'] = $title;
		}
		$res = $db->where('type',1)->paginate(7,false,$where);
		return view("index",compact('res','title'));
	}
	
	/**
	 * 商品审核通过
	 * Method Get
	 * @param string get.id 要审核的商品id
	 * @return json success 通过 error 审核失败错误信息
	 */
	public function through()
	{
		try{
			if(input('post.id')){
				$result = Db::table('product')->where('id',input('post.id'))->update(['status'=>1]);
				if(!$result){
					throw new \PDOException("审核失败");
				}
				$success = ['success'=>'已通过'];
				return json($success);
			}else{
				throw new \PDOException("id不存在");
			}
		}catch (\PDOException $e){
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	/**
	 * 审核不通过
	 * Method Get
	 * @param string post.id 商品id
	 * @return success 操作成功 error 操作失败错误信息
	*/
	public function notthrough()
	{
		try{
			if(input('post.id')){
				$result = Db::table('product')->where('id',input('post.id'))->update(['status'=>5]);
				if(!$result){
					throw new \PDOException("操作失败");
				}
				$success = ['success'=>'操作成功'];
				return json($success);
			}else{
				throw new \PDOException("id不存在");
			}
		}catch (\PDOException $e){
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	/**
	 * 图片查看
	 * Method Get
	 * @param string get.id 产品id
	 *
	 * @return Array result  查询结果集
	*/
	public function image()
	{
		if(input('get.id')){
			$result = Db::table('product')->where('id',input('get.id'))->select();
//			foreach ($result as $k=>$val){
//				$result['image'] = json_decode($val['image']);
//				unset($result[$k]);
//			}
			return view('image',compact('result'));
		}
	}
}